package com.css.dzj.dzjap.modules.construnit.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.HttpClientUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.common.util.userinfo.UserInfoService;
import com.css.dzj.dzjap.modules.construnit.entity.ConstructionUnitEntity;
import com.css.dzj.dzjap.modules.construnit.service.ConstrunitService;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 建筑单位controller
 */
@RequestMapping("/construct")
@RestController
@PropertySource(value = "classpath:base.properties")
public class ConstrunitController {

    @Autowired
    private ConstrunitService construnitService;

    @Autowired
    private EnterpriseService enterpriseService;
    @Value("${SAVE_USER}")
    private String SAVE_USER;
    @Value("${BASE_URL}")
    private String BASE_URL;

    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    HttpServletRequest request;

    /**
     * 建设单位注册
     *
     * @param construct
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody ConstructionUnitEntity construct) {
        String s;
        try {

            Map<String, String> map = new HashMap();
            if (StringUtils.isEmpty(construct.getUniscid()) || StringUtils.isEmpty(construct.getEntName())
                    || StringUtils.isEmpty(construct.getLocation()) || StringUtils.isEmpty(construct.getLegalPerson())
                    || StringUtils.isEmpty(construct.getEntType()) || StringUtils.isEmpty(construct.getLinkMan())
                    || StringUtils.isEmpty(construct.getLinkTel()) || StringUtils.isEmpty(construct.getPassword())
                    || StringUtils.isEmpty(construct.getUserName())) {
                return R.error("必填字段不能为空");
            }
            // 判断该建设单位是否在黑名单
            ApEnterpriseCreditEntity tyReConEnterPrise = enterpriseService.getTyReConEnterPrise("1", construct.getUniscid());
            if (tyReConEnterPrise != null) {
                return R.error("该单位已存在于黑名单中，无法注册!");
            }
            // 判断企业名称是否已存在
            boolean isExistUserName = construnitService.isExistUserName(construct.getUserName());
            if (isExistUserName) {
                return R.error("用户名已存在!");
            }
            // 判断统一社会信用代码是否已存在
            boolean isExistEntCode = construnitService.isExistEntCode(construct.getUniscid());
            if (isExistEntCode) {
                return R.error("统一社会信用代码已存在!");
            }
            // boolean flag=baseService.checkUserExist(construct.getUserName());
            boolean flag = userInfoService.checkUserExist(construct.getUserName(), request);
            if (flag) {
                return R.error("用户名已存在");
            } else {
                //String url = BASE_URL+SAVE_USER;
                if (!StringUtils.isEmpty(construct.getUserName())) {
                    String loginName = construct.getUserName();
                    map.put("loginName", loginName);
                } else {
                    return R.error("用户名不能为空");
                }

                if (!StringUtils.isEmpty(construct.getPassword())) {
                    String password = construct.getPassword();
                    map.put("password", password);
                } else {
                    return R.error("密码不能为空");
                }
                String jianshe = "jianshe";

                //Map msg = baseService.add(construct.getUserName(), jianshe, construct.getPassword());
                // 新base注册
                R r = userInfoService.register(construct.getUserName(), construct.getPassword(), jianshe, request);
                if ("500".equals(r.getCode())){
                    return R.error(r.getMsg());
                }
                construnitService.regist(construct);
                s = JSON.toJSONString(construct);
                sysAuditService.saveSuccessAudit("建设单位管理", Constant.CONSTRUCT,Constant.PT,Constant.ADD,s,request);
                return R.ok();
            }

        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(construct);
            sysAuditService.saveFailAudit("建设单位管理",Constant.CONSTRUCT,Constant.PT,Constant.ADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

}
